require.config({
//	baseUrl: 'http://149.235.139.153:8080/WebViewer/library/js', // Base URL is /js anyway, that is inherited from the index.html, data-main attribute.
//	baseUrl: 'http://' + window.OTCVA.serverName + ':8080/WebViewer/library/js', // Base URL is /js anyway, that is inherited from the index.html, data-main attribute.
	paths: {
		jquery: 'lib/jquery',
		jqueryui: 'lib/jquery-ui',
		underscore: 'lib/underscore',
		backbone: 'lib/backbone',
		marionette: 'lib/backbone.marionette',
		text: 'lib/text'
	},
	shim: {
		'underscore': {
			exports: '_'
		},
		'jquery': {
			exports: '$'
		},
		'jqueryui': {
			deps:['jquery']
		},
		'backbone': {
			deps: ['underscore','jquery'],
			exports: 'Backbone'
		},
		'marionette': {
			deps: ['backbone'],
			exports: 'Marionette'
		}
	}
});

var appCTX = {};
var App = undefined;

require( [
		'underscore', 
		'jquery', 
		'backbone', 
		'marionette', 
		'controller/controller', 
		'controller/router', 
//		'model/sample', 
		'model/list', 
		'model/footnotelist',
//		'view/sample', 
//		'view/grid', 
		'view/footnote',
		'jqueryui' 
	],
// function( _, $, Backbone, Marionette, AppController, AppRouter, SampleModel, SampleCollection, SampleView, GridView ) {
function( _, $, Backbone, Marionette, AppController, AppRouter, SampleCollection, FootnoteCollection, FootnoteView ) {

	appCTX = _.extend( appCTX, Backbone.Events );
	/* Global object for message pumping across the application.
	 * The pattern "ListenTo" allows to register events originating
	 * in other objects, which is a very useful pattern in this app.
	 */

	var myRegion = new Marionette.Region({
		el: '#container'
	});
	// Convenient method for identifying a region (setup an object)

	window.App = new Marionette.Application();

	App.O = [];

	App.addRegions( {
		main: myRegion,
		table: '#grid',
		footnote: '#footnote'
	});

	App.addInitializer(function () {
		console.log("App: addInitializer requested.");
		debugger;
		var controller = new AppController();
		var router = new AppRouter({controller:controller});
		/* This construct works but it does not seem to offer too much beyond classic Backbone router.
		 * The examples I have found for Marionette ususally show the use of Backbone router instead.
		 */

		//var sampleModel = new SampleModel();
		//var sampleView = new SampleView({model:sampleModel}); // this style requires region definition ...
		//App.main.show(sampleView);

		// var list = new SampleCollection(userData);
		var list = new SampleCollection();
		var footnote = new FootnoteCollection();

		App.collection=list;
		//footnote.create({text:'Test Application'});
		//footnote.create({text:'Martin Metal'});
		//footnote.create({text:'(c) 2014'});
		// TODO - do not call this, name collision with region
		App.footnoteCollection=footnote;
		
		//var footnoteView = new FootnoteView({collection: footnote}); 
		//App.footnote.show(footnoteView);

		// var gridView = new GridView({ collection: list });
		// App.table.show(gridView);
	});
	
	App.on('start', function () {
		console.log("App: handler for onInitialize called (initialize:after). -- enter");
		if (Backbone.history) {
			Backbone.history.start();
			console.log("App: handler for onInitialize - initializing History");
		}
		console.log("App: handler for onInitialize called (initialize:after). -- leave");
	});

	App.start();
});
